package com.rmdjzz.slmmn.controller.user.love;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.rmdjzz.slmmn.pojo.base.Love;
import com.rmdjzz.slmmn.pojo.base.Song;
import com.rmdjzz.slmmn.pojo.result.Result;
import com.rmdjzz.slmmn.pojo.vo.PageVo;
import com.rmdjzz.slmmn.pojo.vo.SongVo;
import com.rmdjzz.slmmn.service.LoveService;
import com.rmdjzz.slmmn.service.SingerService;
import com.rmdjzz.slmmn.service.SongService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.models.auth.In;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * @作者 张绍鑫
 * @创建时间 2023/6/7
 */

@Controller
@Api(tags = "收藏")
@RequestMapping("/user")
@Slf4j
public class LoveController {
    @Autowired
    private LoveService loveService;
    @Autowired
    private SongService songService;
    @Autowired
    private SingerService singerService;
    @PostMapping("/love/{userId}/{songId}")
    @ResponseBody
    public String love(@PathVariable Integer userId, @PathVariable Integer songId){
        QueryWrapper<Love> loveQueryWrapper = new QueryWrapper<>();
        loveQueryWrapper.eq("user_id",userId).eq("song_id",songId);
        Love one = loveService.getOne(loveQueryWrapper);
        if (one!=null){
            loveService.removeById(one.getId());
            return "取消收藏成功";
        }else {
            Love love = new Love(userId, songId);
            loveService.save(love);
            return "已收藏";
        }
    }

    /**
     * 根据用户查找收藏
     * 根据收藏用户id查询歌曲
     * 替换list
     * @param userId
     * @return
     */
    @GetMapping("/loveList/{userId}")
    @ResponseBody
    public Result loveList(@PathVariable Integer userId, PageVo pageVo){
        Page<Love> page = new Page<>(pageVo.getStartIndex(),pageVo.getSize());
        QueryWrapper<Love> loveQueryWrapper = new QueryWrapper<>();
        loveQueryWrapper.eq("user_id",userId);
        Page lovePage = loveService.page(page,loveQueryWrapper);
        //替换vo
        List<Love> records = lovePage.getRecords();
        ArrayList songs = new ArrayList<>();
        for (Love love:records){
            //获取歌曲
            Song song = songService.getById(love.getSongId());
            //转vo
            SongVo songVo = new SongVo();
            songVo.setSongName(song.getSongName());
            if (singerService.getById(song.getSingerId())==null){
                songVo.setSingerName("未知歌手");
            }else {
                songVo.setSingerName(singerService.getById(song.getSingerId()).getSingerName());
            }
            songs.add(songVo);
        }
        page.setRecords(songs);
        return Result.ok(lovePage);

    }

    /**
     * 根据userid查询用户收藏
     * 通过用户收藏查询歌曲
     * 将歌曲转为vo
     * @param userId
     * @param model
     * @return
     */
    @GetMapping("/playLoveList/{userId}")
    @ApiOperation("榜单播放")
    public String playRankList(@PathVariable String userId, Model model){

        QueryWrapper<Love> loveQueryWrapper = new QueryWrapper<>();
        loveQueryWrapper.eq("user_id",userId);
        List<Love> loveList = loveService.list(loveQueryWrapper);
        if (loveList.size()==0){
            model.addAttribute("list",new ArrayList<>());
            model.addAttribute("words",new ArrayList<>());
            model.addAttribute("msg","我的收藏");
            log.info("没有找到喜欢的数据");
            return "play";
        }
        ArrayList<SongVo> songVos = new ArrayList<>();
        ArrayList<String> words = new ArrayList<>();
        for (Love love:loveList){
            Song song = songService.getById(love.getSongId());
            SongVo songVo = new SongVo();
            songVo.setId(song.getId());
            songVo.setSongImg(song.getSongImg());

            songVo.setWords(song.getWords());
            songVo.setSongName(song.getSongName());
            songVo.setSongTime(song.getSongTime());
            if (singerService.getById(song.getSingerId())==null){
                songVo.setSingerName("未知歌手");
            }else {
                songVo.setSingerName(singerService.getById(song.getSingerId()).getSingerName());
            }
            songVo.setSong(song.getSong());
            words.add(song.getWords());
            songVos.add(songVo);
        }
        model.addAttribute("list",songVos);
        model.addAttribute("words",words);
        model.addAttribute("msg","我的收藏");
        return "play";
    }

}
